package prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

@SuppressWarnings("serial")
public class Person implements Cloneable,Serializable {
	private String name;
	private int age;
	private Address address;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	
	@Override
	public Person clone() {
		Person person=null;
		try {
		ByteArrayOutputStream bos=new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(this);
		ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
		ObjectInputStream ois = new ObjectInputStream(bis);
		person = (Person) ois.readObject();
		} catch (ClassNotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return person; 
	}
	public static void main(String[] args) {
		Person person=new Person();
		Address address=new Address();
		address.setCity("beijing");
		address.setStreet("chaoyang");
		person.setName("aa");
		person.setAge(11);
		person.setAddress(address);
		Person person2=person.clone();
		System.out.println(person==person2);
		System.out.println(person.getAddress()==person2.getAddress());
		System.out.println(person.getName()==person2.getName());
	}
}
